/**
 * 
 */
package ua.ws.feedsreader.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.repository.RepositoryException;

import ua.ws.feedsreader.common.User;
import ua.ws.feedsreader.db.SemanticManager;
import ua.ws.feedsreader.utils.Log;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


/**
 * @author ricardo
 *
 */
public class ChooseCategories extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6658641430208114432L;

	private List<String> categories;
	private List<String> subscribe = new ArrayList<>();

	@Override
	@Action(results={
			@Result(name="success", location="choosecategories.jsp"),
			@Result(name="gotoFeeds", location="feeds-reader")
	})
	public String execute() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		User user = (User) session.get("USER");
		Log.debug("ChooseCategories: User:"+user.getLogin() );
		SemanticManager semanticManager = new SemanticManager();
		try {
			//carregar as categorias existentes.
			categories = semanticManager.getCategories();
			//carregar as categorias a que o utilizador está inscrito.
			subscribe = semanticManager.getUserCategories( user.getLogin() );
			Log.debug( "ChooseCategories: subscribe size:"+subscribe.size() );
			Log.debug( "ChooseCategories: categories size:"+categories.size() );
			if( subscribe.size() > 0 ) {
				
				Log.debug( "ChooseCategories: subscribe true" );
				for (int j = 0; j < categories.size(); j++) {
					Log.debug( "ChooseCategories: categs " + categories.get(j) );
				}
				
				
				for (int i = categories.size()-1; i >= 0; i--) {
					Log.debug( "ChooseCategories: "+ categories.get(i) + " - " + subscribe.contains( categories.get(i) ) );
					if( subscribe.contains( categories.get(i) ) ) {
						categories.remove(i);
					}
				}
				
			}
			
			return SUCCESS;
		} catch (RepositoryException | MalformedQueryException | QueryEvaluationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ERROR;
		}
	}
	
	@Action(value="saveCategories", results={ @Result(name="success", location="feeds-reader", type="redirect")} )
	public String saveCategoriesSubscribed() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		User user = (User) session.get("USER");
		SemanticManager semanticManager = new SemanticManager();
		try {
			semanticManager.subscribeCategories(user.getLogin(), subscribe);
		} catch (RepositoryException e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * @return the categories
	 */
	public List<String> getCategories() {
		return categories;
	}

	/**
	 * @param categories the categories to set
	 */
	public void setCategories(List<String> categories) {
		this.categories = categories;
	}

	/**
	 * @return the subscribe
	 */
	public List<String> getSubscribe() {
		return subscribe;
	}

	/**
	 * @param subscribe the subscribe to set
	 */
	public void setSubscribe(List<String> subscribe) {
		this.subscribe = subscribe;
	}

}
